# Contributor: Marian <marian.buschsieweke@ovgu.de>
# Maintainer: Marian <marian.buschsieweke@ovgu.de>
pkgname=afl++
pkgver=4.20c
pkgrel=0
pkgdesc="Fuzzer relying on genetic algorithms instead of brute force"
url="https://aflplus.plus/"
# s390x: blocked by missing lld
# x86: basic unit tests fails
# 32-bit: unlikely anyone is doing fuzzing here
arch="all !armhf !armv7 !s390x !x86"
license="Apache-2.0"
# afl-cmin requires stat
_llvmver=17
# llvm LTO test fails on riscv64
# https://github.com/AFLplusplus/AFLplusplus/issues/2064
case "$CARCH" in
	riscv64) options="$options !check";;
esac
depends="
	clang$_llvmver
	compiler-rt
	lld~$_llvmver
	llvm$_llvmver
	python3
	"
# install -T
makedepends="
	clang$_llvmver-dev
	gmp-dev
	grep
	llvm$_llvmver-dev
	python3-dev
	"
checkdepends="
	bash
	cmocka-dev
	coreutils
	grep
	"
subpackages="
	$pkgname-doc
	afl++-tools
	"
provides="
	afl=$pkgver-r$pkgrel
	afl-clang=$pkgver-r$pkgrel
	afl-gcc=$pkgver-r$pkgrel
	"
source="
	$pkgname-$pkgver.tar.gz::https://github.com/AFLplusplus/AFLplusplus/archive/refs/tags/v$pkgver.tar.gz
	"
builddir="$srcdir/AFLplusplus-$pkgver"

build() {
	export PATH="$PATH:/usr/lib/llvm$_llvmver/bin"
	export NO_NYX=1
	export AFL_NO_X86=1
	export LLVM_LTO=1
	make PREFIX=/usr all
}

check() {
	# Unset our CFLAGS/CXXFLAGS for the tests since these may
	# interact in unexpected ways with afl-cc instrumentation.
	CFLAGS= CXXFLAGS= make AFL_NO_X86=1 test
}

package() {
	make AFL_NO_X86=1 PREFIX=/usr DESTDIR="$pkgdir" install

	# Test cases contain x86 binaries that cause trouble with strip on non-x86
	# architectures, so just drop the test cases here.
	rm -rf "$pkgdir"/usr/share/afl/testcases/

	# afl-cmin.bash is the same as afl-cmin, but implemented differently making
	# use of bash features. No need for the same functionality twice.
	rm "$pkgdir"/usr/bin/afl-cmin.bash
}

tools() {
	pkgdesc="AFL++ tools and utilities"
	depends="
		afl++
		coreutils
		grep
		"

	amove usr/bin/afl-cmin
	amove usr/bin/afl-plot
	amove usr/bin/afl-showmap
	amove usr/bin/afl-tmin
}

sha512sums="
b126b65d5a3054d1ea0a28333e81ae47e1bb2fb2f411e9b8edbe547b2962d8e58e1dbede7ea957c0572cc91d2912f3e5465f1e835e1ae072e3ccc31b33e555f5  afl++-4.20c.tar.gz
"
